var G_workPosit = "1";  //문서 위치

//## 레이어 오픈  ####################
var G_curLayer = "List";
function fnShowLyr()
{
	var h = "";
	if (G_curLayer == "Contents") 
	{
		h = $("#layerList").css("height").replace("px","");
		h = parseInt(h,10) + 19;
		
		$("#layerContents").css("z-index","-4");
		$("#layerContents").css("top","173px");

		$("#layerCover").css("z-index","-5");
		$("#layerCover").css("top","175px");
		$("#layerCover").css("left","10px");
		$("#layerCover").css("height",h+"px");
		$("#layerCover").css("width","820px");
		$("#layerCover").css("display","");
		
		$("#layerList").css("z-index","-10");
		$("#layerList").css("top","175px");
		
		G_curLayer = "List";
	}
	else
	{
		h = $("#layerContents").css("height").replace("px","");
		h = parseInt(h,10) + 19;
		
		$("#layerList").css("z-index","-4");
		$("#layerList").css("top","173px");

		$("#layerCover").css("z-index","-5");
		$("#layerCover").css("top","175px");
		$("#layerCover").css("left","290px");
		$("#layerCover").css("height",h+"px");
		$("#layerCover").css("width","820px");
		$("#layerCover").css("display","");
		
		$("#layerContents").css("z-index","-10");
		$("#layerContents").css("top","175px");
		
		G_curLayer = "Contents";
	}
}

//## 의견(댓글) 작성 ########################
function fnSaveTail()
{
	if (!$("#seq").val())
	{
		alert("게시물을 먼저 선택해 주세요.");
		return;
	}
	if (!$("#comment").val())
	{
		alert("의견을 바르게 입력해 주세요");
		$("#comment").focus();
		return;
	}

	var valUrl = '/action/groupBbs/saveTail.asp';
	var param  = "flag=Y";
		param += "&comment="+encodeURIComponent($("#comment").val());
		param += "&seq="+$("#seq").val();
	
	if (xMode=="Debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			fnSessionCheck($(xml).find("MSG").text());
			if ($(xml).find("MSG").text() == "Save OK!")
			{
				alert("Save OK!");
				fnGetTail();
				fnTailReset();
			}
			else alert($(xml).find("MSG").text());
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 수신문서 그리드 ##################################################

var G_curPage01 = 1;

function grid01List(page)
{		
	G_workPosit = "1";
	var gridNo = 1;
	var xmlNode = "Bbs";

	fnResetGrid(gridNo);
	fnNewLoading(gridNo);

	if (!page) page = G_curPage01;
	if (page == undefined) page = 1;
	else G_curPage01 = page;
	var limit = 50;

	var valUrl = "/action/groupBbs/list.asp";

	var Parm = "findWord="+encodeURIComponent($("#findWord").val());
		
	if (xMode=="Debug") window.open(valUrl+'?xMode=debug&'+Parm);

	// 그리드 헤더를 기준으로 Cell의 폭을 구해온다.
	var cellWidth = fnGetGridCellWidth(gridNo);	
	// Cell의 정렬방식
	var cellAlign = ",left,,,,,".split(",");
	// Cell의 링크 걸지 여부 결정
	var cellLink = ",,,,,,".split(",");

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: Parm,
		success: function(xml){
			var gridList = "";
			var k = 1;
			var ttlCnt = $(xml).find(xmlNode).length;
			if (ttlCnt%limit == 0) var totalPage = parseInt((ttlCnt / limit),0);
			else 	var totalPage = parseInt((ttlCnt / limit),0)  + 1;
			var cPageNo = (totalPage) * limit;
			if (cPageNo > ttlCnt) cPageNo = ttlCnt;
			
			var startNo = (parseInt(page,10)-1) * limit;
			var endNo = parseInt(page,10) * limit;
			var bgColor = "#FFF";
			var chkCnt = 0;
			
			// 그리드의 부가 정보(레코드수, 페이징) 표시 또는 가리기
			fnGridExtraInfo(gridNo, ttlCnt, page, limit); 

			if (ttlCnt <= endNo) endNo = ttlCnt;
			var no = endNo;
			var empty = "&nbsp;";
			var oldapprSeq = "";
			var style = "";
			$(xml).find(xmlNode).each(function(idx){

				no = cPageNo;
				if (k>startNo && k<=endNo)
				{					
					var seq		= $(this).find("seq").text();
					var titles			= $(this).find("titles").text();
					var hit		= $(this).find("hit").text();
					var userNm			= $(this).find("userNm").text();
					var wDate		= $(this).find("wDate").text();
					var tailCnt			= $(this).find("tailCnt").text();

					if (tailCnt) tailCnt = " <font style='color:#cc0000;font-weight:normal;'>("+tailCnt+")</font>";

					var arrVal = new Array();
					arrVal[0] = "<p>"+seq+"</p>";
					arrVal[1] = "<p>"+titles+tailCnt+"</p>";
					arrVal[2] = "<p>"+userNm+"</p>";
					arrVal[3] = "<p>"+wDate+"</p>";
					arrVal[4] = "<p"+hit+"</p>";
															
					var link = "fnShowBbs('"+seq+"',G_uid)";
					gridList = fnDrawCell(arrVal, bgColor, cellWidth, cellAlign, '', link, cellLink);

					$("#"+"gridBody"+gridNo).append(gridList);
				}
				cPageNo--;
				k++;			
			});
		},
		error: function(xml){
			alert('Error!!\r\n'+xml.responseText);
		}
	});
}


//## 의견 가져오기 ######################################
function fnGetTail()
{		
	var valUrl = "/action/groupBbs/listTail.asp";

	var Parm = "seq="+$("#seq").val();
	
	if (xMode=="Debug") window.open(valUrl+'?xMode=debug&'+Parm);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: Parm,
		success: function(xml){
			
			var rslt = "";
			var k = 0;
			$("#showTails").html("");
			$(xml).find("Tails").each(function(idx){
				
				var tailSeq		= $(this).find("tailSeq").text();
				var comment	= $(this).find("comment").text();
				var tailer			= $(this).find("tailer").text();
				var tailerDuty			= $(this).find("tailerDuty").text();
				var wDt			= $(this).find("wDt").text();
					
				if (k==0)
				{
					rslt = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"798\"><tr><td height=\"10\"></td></tr></table>";
					rslt += "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"798\"><tr>";
					rslt += "<td class=\"lTD_t0bl\" width=\"130\" height=\"50\" style=\"background-color:#eaf2fd;text-align:center;\"><b>"+tailer+"</b> "+tailerDuty+" 의견<BR>"+wDt.split("-").join(".")+"</td>";
					rslt += "<td class=\"lTD_trb0\" width=\"668\" style=\"padding: 7px 15px 7px 15px;text-align:left\">"+comment.split("\n").join("<br>")+"</td></tr></table>";
				}
				else
				{
					rslt = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"798\"><tr>";
					rslt += "<td class=\"lTD_00bl\" width=\"130\" height=\"50\" style=\"background-color:#eaf2fd;text-align:center;\"><b>"+tailer+"</b> "+tailerDuty+" 의견<BR>"+wDt.split("-").join(".")+"</td>";
					rslt += "<td class=\"lTD_0rb0\" width=\"668\" style=\"padding: 7px 15px 7px 15px;text-align:left\">"+comment.split("\n").join("<br>")+"</td></tr></table>";
				}
				if (rslt) { $("#showTails").append(rslt); }
				k++;
			});
		},
		error: function(xml){
			alert('Error!!\r\n'+xml.responseText);
		}
	});
}


function fnPrint()
{		
	var printWin = window.open("","","");
	
	var winTxt = "";
	winTxt = "<HTML>";
	winTxt += "<META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n";
	winTxt += "<meta http-equiv=\"Cache-Control\" content=\"no-cache\">\n";
	winTxt += "<meta http-equiv=\"Pragma\" content=\"no-cache\">\n";
	winTxt += "<script language = \"JavaScript\" src=\"/inc/js/ScriptX_TAG_SCM.js\"></script>\n";
	winTxt += "<script>initScriptX(\"contexts\", \"100%\", \"100%\"); </script>\n";
	winTxt += "<script for=window event=onload>\n";
	winTxt += "	printWindow(document.contexts);\n";
		
	winTxt += "	function printWindow(objName) {\n";
	winTxt += "		objName.printing.header = '';\n";
	winTxt += "		objName.printing.footer = '';\n";
	winTxt += "		objName.printing.portrait = true; // false 는 가로인쇄\n";
	winTxt += "		objName.printing.leftMargin = 15.0;\n";
	winTxt += "		objName.printing.topMargin = 15.0;\n";
	winTxt += "		objName.printing.rightMargin = 15.0;\n";
	winTxt += "		objName.printing.bottomMargin = 1.0;\n";
	winTxt += "		objName.printing.Print(false);\n";
	winTxt += "	}\n";
	winTxt += "</script>\n";
	winTxt += "<"+"style"+">";
	winTxt += "td, span, input, select, textarea, th { font-family: \"맑은 고딕, tahoma\"; font-size: 14px; letter-spacing:0; }";
	winTxt += "span { font-family: \"맑은 고딕, tahoma\"; font-size: 14px; letter-spacing:0; }";
	winTxt += ".lTD_trbl { BORDER-TOP: #abccf8 1px solid; BORDER-RIGHT: #abccf8 1px solid; BORDER-BOTTOM: #abccf8 1px solid; BORDER-LEFT: #abccf8 1px solid;}";
	winTxt += ".lTD_0rbl { BORDER-TOP: #abccf8 0px solid; BORDER-RIGHT: #abccf8 1px solid; BORDER-BOTTOM: #abccf8 1px solid; BORDER-LEFT: #abccf8 1px solid;}";
	winTxt += ".lTD_00b0 { BORDER-TOP: #abccf8 0px solid; BORDER-RIGHT: #abccf8 0px solid; BORDER-BOTTOM: #abccf8 1px solid; BORDER-LEFT: #abccf8 0px solid;}";
	winTxt += ".lTD_t00l { BORDER-TOP: #abccf8 1px solid; BORDER-RIGHT: #abccf8 0px solid; BORDER-BOTTOM: #abccf8 0px solid; BORDER-LEFT: #abccf8 1px solid; font-weight:bold;}";
	winTxt += ".lTD_t0bl { BORDER-TOP: #abccf8 1px solid; BORDER-RIGHT: #abccf8 0px solid; BORDER-BOTTOM: #abccf8 1px solid; BORDER-LEFT: #abccf8 1px solid;}";
	winTxt += ".lTD_t0blBold { BORDER-TOP: #abccf8 1px solid; BORDER-RIGHT: #abccf8 0px solid; BORDER-BOTTOM: #abccf8 1px solid; BORDER-LEFT: #abccf8 1px solid; font-weight:bold;}";
	winTxt += ".lTD_t0b0 { BORDER-TOP: #abccf8 1px solid; BORDER-RIGHT: #abccf8 0px solid; BORDER-BOTTOM: #abccf8 1px solid; BORDER-LEFT: #abccf8 0px solid;}";
	winTxt += ".lTD_tr0l { BORDER-TOP: #abccf8 1px solid; BORDER-RIGHT: #abccf8 1px solid; BORDER-BOTTOM: #abccf8 0px solid; BORDER-LEFT: #abccf8 1px solid;}";
	winTxt += ".lTD_trb0 { BORDER-TOP: #abccf8 1px solid; BORDER-RIGHT: #abccf8 1px solid; BORDER-BOTTOM: #abccf8 1px solid; BORDER-LEFT: #abccf8 0px solid;}";
	winTxt += ".lTD_00bl { BORDER-TOP: #abccf8 0px solid; BORDER-RIGHT: #abccf8 0px solid; BORDER-BOTTOM: #abccf8 1px solid; BORDER-LEFT: #abccf8 1px solid;}";
	winTxt += ".lTD_0rb0 { BORDER-TOP: #abccf8 0px solid; BORDER-RIGHT: #abccf8 1px solid; BORDER-BOTTOM: #abccf8 1px solid; BORDER-LEFT: #abccf8 0px solid;}";
	winTxt += ".lTD_000l { BORDER-TOP: #abccf8 0px solid; BORDER-RIGHT: #abccf8 0px solid; BORDER-BOTTOM: #abccf8 0px solid; BORDER-LEFT: #abccf8 1px solid;}";
	winTxt += ".lTD_tr00 { BORDER-TOP: #abccf8 1px solid; BORDER-RIGHT: #abccf8 1px solid; BORDER-BOTTOM: #abccf8 0px solid; BORDER-LEFT: #abccf8 0px solid;}";
	winTxt += ".lTD_0r00 { BORDER-TOP: #abccf8 0px solid; BORDER-RIGHT: #abccf8 1px solid; BORDER-BOTTOM: #abccf8 0px solid; BORDER-LEFT: #abccf8 0px solid;}";
	winTxt += ".lTD_t000 { BORDER-TOP: #abccf8 1px solid; BORDER-RIGHT: #abccf8 0px solid; BORDER-BOTTOM: #abccf8 0px solid; BORDER-LEFT: #abccf8 0px solid;}";
	winTxt += "</"+"style"+">";
	winTxt += "<body>";
	var printTxt = $("#layerContents").html().replace(buttons.innerHTML,"&nbsp;");
	printTxt = printTxt.replace(addTail.innerHTML,"");
	winTxt += printTxt;
	//winTxt += "<script>window.print(); </script>";
	winTxt += "</body>";
	winTxt += "</HTML>";
	printWin.document.write(winTxt);
	//setTimeout("printWin.print();",1000);
}

//## 전자결재 상세 정보 ####
function fnShowBbs(seq, rcvrId, showFlag)
{
	if (G_workPosit == 1) var detailTitle = "1. 자유게시판 내용";
	
	var valUrl = "/action/groupBbs/view.asp";
	var param = "seq="+seq;

	if (xMode=="Debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());
			fnReset();
			$("#contentsLabel").val(detailTitle);
			$("#seq").val($(xml).find("seq").text());
			$("#userId").val($(xml).find("userId").text());
			var title = $(xml).find("titles").text();
			$("#subjects").html(title);
			$("#sndrNm").html("작성자 : "+$(xml).find("userNm").text());
			$("#writeDt").html("작성시간 : "+$(xml).find("wDate").text());
			$("#contents").html($(xml).find("contents").text());
			var fileLink = "";
			if ($(xml).find("files").text()) fileLink = "<a href='/files/"+$(xml).find("files").text()+"'>"+$(xml).find("files").text()+"</a>";
			$("#files").html(fileLink);
			
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
	if (showFlag == undefined ) fnShowLyr();
	setTimeout("fnGetTail();",500);
}

//## 전자결재 작성 및 수정, 전달
function fnOpenBbsWrite(f,mode)
{
	if (f != "" && G_uid != $("#userId").val() && mode != "pass")
	{
		alert("당신은 작성자가 아닙니다.\n\r작성자만 수정할 수 있습니다.");
		return false;
	}
	window.open("./groupBbsWrite.asp?seq="+f+"&m="+mode, "groupAppr", "width=820,height=610,scrollbars=1,menubar=no");
}

//## 전자결재 상세정보 Reset 기능
function fnReset()
{
	$("#contentsLabel").val("");
	$("#seq").val("");
	$("#userId").val("");
	$("#subjects").html("");
	$("#writeDt").html("");
	$("#contents").html("");
	$("#files").html("");
	$("#showTails").html("");
}

//## 의견 달기 Result 기능
function fnTailReset()
{
	$("#comment").val("");
}

//## 검색 Reset 기능
function fnSearchReset()
{
	$("#findWord").val("");
}